import java.io.File

fun main(args: Array<String>) {
    // 首先在RDMP中创建数据字典：中文名称为“所属机构”、英文简称为“SSJG”，然后用其主键生成glossary_item表的具体数据

    val orgFilepath = "org.txt"
    val orgFileLines = File(orgFilepath).readLines(charset = Charsets.UTF_8)
    if (orgFileLines.isEmpty()) {
        throw RuntimeException("机构数据源为空!")
    }
    val companyFilepath = "company.txt"
    var companyFileLines = File(companyFilepath).readLines(charset = Charsets.UTF_8)
    if (companyFileLines.isEmpty()) {
        throw RuntimeException("公司数据源为空!")
    }
    val allLines: MutableList<String> = mutableListOf()
    allLines.addAll(orgFileLines)
    allLines.addAll(companyFileLines)
    val distinctAndSortedList = allLines.filter { it.isNotEmpty() }.distinct().sorted()

    val range = 1..distinctAndSortedList.size
    val userId = 2
    val glossaryId = 70

    val sqls: List<String> = mutableListOf()

    range.forEach {
        val index = it - 1
        val orgName = distinctAndSortedList[index]
        val sqlFragment = """
            ($userId, $userId, $it, '$orgName', $it, $glossaryId)${if (it != range.last) """, ${System.lineSeparator()}""" else ""}
        """.trimIndent()
        sqls.addLast(sqlFragment)
    }

    if (sqls.isNotEmpty()) {
        val sql = """
            INSERT INTO glossary_item (created_by, updated_by, item_code, item_message, order_sort, glossary_id)
            VALUES ${System.lineSeparator()}
        """.trimIndent()
        sqls.addFirst(sql)
        sqls.addLast(";")
    } else {
        throw RuntimeException("sqls is empty!")
    }

    val resultSql = sqls.joinToString(separator = "")
    val sqlPath = "insert_into_glossary_item.sql"
    File(sqlPath).writeText(resultSql)
}